import HttpUtils from '../HttpUtils/HttpUtils';
// 获取当前时间戳
var timestamp = Date.parse(new Date()) / 1000;
let globalData = getApp().globalData;

Page({
	...HttpUtils.func,
	data: {
		pageDetail: 'articleDetail',
		player: 'playerHide',
		pageIndex: 1,
		pageItems: 3,
		totalPages: 0,
		newsItems: [],
		// 党建电视
		studyPageConst: {
			//本月推荐栏目ID
			yjyyColumnId: 8129,
			//党建电视栏目ID
			djtvColumnId: 8130,
			//精选专题栏目ID
			topicColumnId: 8137,
		},
		topicList: [],
		djtvColumnList: [],
		yjyyColumnList: [],
		kczxColumnList: []
	},
	onLoad() {
		this.loadNewsList();
	},
	// 获取本月推荐List
	loadNewsList: function() {
		let folderCode = this.data.studyPageConst.yjyyColumnId,
		    newUrl = globalData.dangjianUrl + '/content/relativeContentQuery',
		    params = {};

		params["pageIndex"] = this.data.pageIndex + '';
		params["pageItems"] = this.data.pageItems + '';
		params["code"] = folderCode + '';
		params["siteCode"] = globalData.siteCode + '';
		params = JSON.stringify(params);
		this.httpGet(newUrl, params, "支部新闻列表加载失败").then(
			(respData) => {
				var result = respData.data.result.code;
				if (1 == result) {
					var contents = respData.data.contents;
					var totalPages = contents.totalPages;
					var pageIndex = contents.pageIndex;
					contents = contents.content;
					var contentList = [];
					for (var index in contents) {
						var content = contents[index];
						var json = {};
						var thumbnail = content.thumbnail;
						if (null == thumbnail || "" == thumbnail) {
							var imageFiles = content.imageFiles;
							for (var j in imageFiles) {
								if (imageFiles[j].type == getApp().globalData.imageType) {
									thumbnail = imageFiles[j].url;
									break;
								}
							}
						}
						json["name"] = content.name;
						json["publishTime"] = content.publishTime;
						json["url"] = thumbnail;
						json["code"] = content.code;
						json["type"] = content.type;
						json["foldCodes"] = folderCode;
						json["count"] = content.count;
						json["fromNa"] = '' == content.fromNa ? "华数": content.fromNa;
						json["folderCode"] = folderCode;
						contentList.push(json);
					}
					var columnList = this.data.columnList;
                    if (columnList && columnList.length) {
                        for (var index in columnList) {
                            var column = columnList[index];
                            if (folderCode == column.code) {
                                column["className"] = "titOn";
                            } else {
                                column["className"] = "";
                            }
                        }
                        this.setData({
                            "columnList": columnList
                        })
                    }
					
					this.setData({
						"contentList": contentList,
						"totalPages": totalPages,
						"pageIndex": pageIndex,
						"currentFolderCode": folderCode
					});

				} else {
					wx.alert({ title: respData.data.result.description });
				}
				//加载党建电视子栏目
				this.loadDjtvColumnList();
			}
		)
	},

	//加载党建电视子栏目 内容
	loadDjtvColumnList: function() {
		var parentId = this.data.studyPageConst.djtvColumnId;
		var newUrl = getApp().globalData.dangjianUrl + "/content/relativeFolderQuery";
 
		var params = {};
		params["pageIndex"] = "1";
		params["pageItems"] = "4";
		params["code"] = parentId + '';
		params["siteCode"] = getApp().globalData.siteCode + "";
		params = JSON.stringify(params);
		this.httpGet(newUrl, params, "子栏目加载失败").then(
			(respData) => {
				var result = respData.data.result.code;
				if (1 == result) {
					var folders = respData.data.folders.folder;
					this.setData({
						"djtvColumnList": folders
					});
				} else {
					wx.alert({ title: respData.data.result.description });
				}
				//加载精选专题子栏目  20190710 （精选专题）还没有先用远程教育体现效果
				this.loadYcjyColumnList(this.data.studyPageConst.yjyyColumnId);
			}
		);
	},

	// 精选专题
	loadYcjyColumnList: function(e) {
		//加载栏目下内容
		var folderCode = this.data.studyPageConst.topicColumnId;
		var newUrl = getApp().globalData.dangjianUrl + "/content/relativeContentQuery";
		var params = {};
		params["pageIndex"] = "1";
		params["pageItems"] = "2";
		params["code"] = folderCode + "";
		params["siteCode"] = getApp().globalData.siteCode + "";
		params = JSON.stringify(params);
		this.httpGet(newUrl, params, "精选专题下内容加载失败").then(
			(respData) => {
				var result = respData.data.result.code;
				if (1 == result) {
					var contents = respData.data.contents.content;
					var contentList = [];
					for (var index in contents) {
						var content = contents[index];
						var json = {};
						var thumbnail = content.thumbnail;
						if (null == thumbnail || "" == thumbnail) {
							var imageFiles = content.imageFiles;
							for (var j in imageFiles) {
								thumbnail = imageFiles[j].url;
								break;
							}
						}
						json["name"] = content.name;
						json["publishTime"] = content.publishTime;
						json["url"] = thumbnail;
						json["code"] = content.code;
						json["type"] = content.type;
						json["foldCode"] = folderCode;
						json["count"] = content.count;
						json["fromNa"] = '' == content.fromNa ? "华数" : content.fromNa;
						json["folderCode"] = folderCode;
						contentList.push(json);
					}
					this.setData({
						"topicList": contentList
					});
				} else {
					wx.alert({ title: respData.data.result.description });
				}
			}
		);
	},


	//栏目内容详情展示
	articleDetail(e) {
		var folderCode = e.currentTarget.dataset.foldercode;
		var type = e.currentTarget.dataset.type;
		var code = e.currentTarget.dataset.code;
		var name = e.currentTarget.dataset.name;
		var detailUrl = this.getUrl(type, code, folderCode, name);
		if (detailUrl.length > 0) {
			wx.navigateTo({
				url: detailUrl
			});
		} else {
			wx.alert({ title: "该内容类型详情页暂未开发" });
		}
	},

	//查看更多的跳转
	forwardList: function(e) {
		var folderCode = e.currentTarget.dataset.foldercode;
		var currentFolderCode = e.currentTarget.dataset.currentcode;
		wx.navigateTo({
			url: "/pages/recommend/recommend?folderCode=" + folderCode + "&currentFolderCode=" + currentFolderCode
		});
	},

	//党建电视跳转
	shortVideo(e) {  //党建电视
		var folderCode = e.currentTarget.dataset.code;
		wx.navigateTo({
			url: '../shortVideo/shortVideo?folderCode=' + folderCode
		})
	}

});
